package com.sds.system.services;


import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.sds.system.daos.AbstractDAO;
import com.sds.system.models.IdentifiedObject;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

public abstract class AbstractDAOService {
    private Class<? extends IdentifiedObject> objectClass;
    private AbstractDAO objectDao;
    private Gson gson = new Gson();

    protected AbstractDAOService(Class<? extends IdentifiedObject> objectClass, AbstractDAO objectDao) {
        this.objectClass = objectClass;
        this.objectDao = objectDao;
    }

    @GET
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public String getAll() {
        return gson.toJson(Sets.newHashSet(objectDao.getObjectManager(objectClass).findAll().perform().getCollectionResult()));
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public String getById(@PathParam("id") String id) {
        return gson.toJson(objectDao.getObjectManager(objectClass).findById(id).perform().getSingleResult());
    }
}
